package gov.child.web.controller;

import gov.child.entities.vo.*;
import gov.child.service.*;
import gov.child.util.FileSaver;
import gov.child.util.LocalCode;
import gov.child.util.StringUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author fzh
 * @since 2017/9/16
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private PersonalDeclareFormService personalDeclareFormService;

    @Autowired
    private EnterpriseDeclareFormService enterpriseDeclareFormService;

    @Autowired
    private GuideService guideService;

    @GetMapping
    public ModelAndView login() {
        return new ModelAndView("login");
    }

    //***
    // 登陆校验
    //-----------------------//
    @PostMapping("/check")
    public ModelAndView check(@RequestParam String username, @RequestParam String password, HttpSession session) {
        Admin admin = adminService.findByUsernameAndPassword(username, password);
        ModelAndView mv;
        if (admin.equals(Admin.EMPTY_ADMIN)) {
            // 没有该管理员
            mv = new ModelAndView("forward:/admin");
            mv.addObject("message", "请核对用户名或密码");
        } else {
            session.setAttribute("admin", admin);
            Department department = departmentService.findById(admin.getDeptId());
            session.setAttribute("dept", department);
            mv = new ModelAndView("forward:/admin/guide/manage?option=all&start=1");
        }
        return mv;
    }

    //***
    // 菜单
    //-----------------------//
    @RequestMapping(value = "/{submenu}/manage")
    public ModelAndView manage(@PathVariable String submenu, @RequestParam String option, Integer start) {
        ModelAndView mv = new ModelAndView("manage");
        mv.addObject("submenu", submenu);
        if (Objects.nonNull(start)) mv.addObject("start", start);
        return mv;
    }

    //***
    // 内容面板
    //-----------------------//
    @RequestMapping("/pane")
    public ModelAndView menuDispatcher(@RequestParam String submenu, @RequestParam String option, Integer start, HttpSession session) {
        if ("guide".equals(submenu)) {
            return guide(option, start, session);
        } else if ("person".equals(submenu)) {
            return person(option, start, session);
        } else if ("enterprise".equals(submenu)) {
            return enterprise(option, start, session);
        } else {
            throw new RuntimeException("submenu error");
        }
    }

    private ModelAndView guide(String option, Integer start, HttpSession session) {
        Department department = (Department) session.getAttribute("dept");
        ModelAndView mv = new ModelAndView();
        if ("add".equals(option)) {
            mv.setViewName("guide_add");
            List c = departmentService.findByDepartmentParentId(department.getDepartmentId());
            session.setAttribute("childDepts", c);
        } else if ("all".equals(option)) {
            Page page = guideService.getPage(20);
            page.setCurrentPage(start);
            List result = guideService.findByDepartment(department, page);
            mv.setViewName("guide_all");
            mv.addObject("result", result);
            mv.addObject("page", page);
        } else {
            throw new RuntimeException("option error");
        }
        return mv;
    }

    private ModelAndView person(String option, Integer start, HttpSession session) {
        Department department = (Department) session.getAttribute("dept");
        ModelAndView mv = new ModelAndView();
        List result;
        Object o = session.getAttribute("page");
        Page page = o == null ? PageUtil.createPage(20, 100, start) : (Page) o;
        if ("new".equals(option)) {
            mv.setViewName("person_form_new");
            result = personalDeclareFormService.findByDepartmentAndStatus(department, (byte) 0, page);
            page.setTotalCount(result.size());
            session.setAttribute("page", page);
            mv.addObject("result", result);
        } else if ("all".equals(option)) {
            mv.setViewName("person_form_all");
            result = personalDeclareFormService.findByDepartment(department, page);
            page.setTotalCount(result.size());
            session.setAttribute("page", page);
            mv.addObject("result", result);
        } else {
            throw new RuntimeException("option error");
        }
        return mv;
    }

    private ModelAndView enterprise(String option, Integer start, HttpSession session) {
        Department department = (Department) session.getAttribute("dept");
        ModelAndView mv = new ModelAndView();
        List result;
        Object o = session.getAttribute("page");
        Page page = o == null ? PageUtil.createPage(20, 100, start) : (Page) o;
        if ("new".equals(option)) {
            mv.setViewName("enterprise_form_new");
            result = enterpriseDeclareFormService.findByDepartmentAndStatus(department, (byte) 0, page);
            page.setTotalCount(result.size());
            session.setAttribute("page", page);
            mv.addObject("result", result);
        } else if ("all".equals(option)) {
            mv.setViewName("enterprise_form_all");
            result = enterpriseDeclareFormService.findByDepartment(department, page);
            page.setTotalCount(result.size());
            session.setAttribute("page", page);
            mv.addObject("result", result);
        } else {
            throw new RuntimeException("option error");
        }
        return mv;
    }

    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/admin";
    }
}
